package site.marqstree.kotlin.library.base.ui.widget.bottomnavbar

import android.content.Context
import android.util.AttributeSet
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import com.ashokvarma.bottomnavigation.ShapeBadgeItem
import com.ashokvarma.bottomnavigation.TextBadgeItem
import site.marqstree.kotlin.library.base.ui.R


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.baselibrary.widget
 * 文件名: BottomNavBar
 * 创建者: marqstree
 * 创建时间: 2020/2/24 14:59
 * 描述: 底部Tab页导航按钮栏
 * 包含功能：
 * 1）高亮选中导航按钮
 * 2）可显隐按钮右上角文字角标
 */
class BottomNavBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BottomNavigationBar(context, attrs, defStyleAttr) {

    //购物车Tab 标签
    val mCartBadge: TextBadgeItem
    //消息Tab 标签
    val mMsgBadge: ShapeBadgeItem

    init {
        //首页
        val homeItem = BottomNavigationItem(R.drawable.btn_nav_home_press,
                resources.getString(R.string.nav_bar_home))
            .setInactiveIconResource(R.drawable.btn_nav_home_normal)
            .setActiveColorResource(R.color.common_blue)
            .setInActiveColorResource(R.color.text_normal)

        //分类
        val categoryItem = BottomNavigationItem(R.drawable.btn_nav_category_press,
                resources.getString(R.string.nav_bar_category))
            .setInactiveIconResource(R.drawable.btn_nav_category_normal)
            .setActiveColorResource(R.color.common_blue)
            .setInActiveColorResource(R.color.text_normal)

        //购物车
        val cartItem = BottomNavigationItem(R.drawable.btn_nav_cart_press,
                resources.getString(R.string.nav_bar_cart))
            .setInactiveIconResource(R.drawable.btn_nav_cart_normal)
            .setActiveColorResource(R.color.common_blue)
            .setInActiveColorResource(R.color.text_normal)
        //添加购物车右上角文字徽章
        mCartBadge = TextBadgeItem()
        cartItem.setBadgeItem(mCartBadge)
        mCartBadge.setText("10")

        //消息
        val msgItem = BottomNavigationItem(R.drawable.btn_nav_msg_press,
                resources.getString(R.string.nav_bar_msg))
            .setInactiveIconResource(R.drawable.btn_nav_msg_normal)
            .setActiveColorResource(R.color.common_blue)
            .setInActiveColorResource(R.color.text_normal)
        //添加消息右上角形状徽章
        mMsgBadge = ShapeBadgeItem()
        mMsgBadge.setShape(ShapeBadgeItem.SHAPE_OVAL)
        msgItem.setBadgeItem(mMsgBadge)

        //我的
        val userItem = BottomNavigationItem(R.drawable.btn_nav_user_press,
                resources.getString(R.string.nav_bar_user))
            .setInactiveIconResource(R.drawable.btn_nav_user_normal)
            .setActiveColorResource(R.color.common_blue)
            .setInActiveColorResource(R.color.text_normal)

        //设置底部导航模式及样式
        setMode(BottomNavigationBar.MODE_FIXED)
        setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC)
        setBarBackgroundColor(R.color.common_white)
        //添加Tab
        addItem(homeItem)
            .addItem(categoryItem)
            .addItem(cartItem)
            .addItem(msgItem)
            .addItem(userItem)
            .setFirstSelectedPosition(0)
            .initialise()
    }

    /*
        检查购物车Tab是否显示文字徽章
        count:数量
     */
    fun checkCartBadge(count:Int){
        if (count == 0){
            mCartBadge.hide()
        }else{
            mCartBadge.show()
            mCartBadge.setText("$count")
        }
    }

    /*
        检查消息Tab是否显示形状徽章
     */
    fun checkMsgBadge(isVisiable:Boolean){
        if (isVisiable){
            mMsgBadge.show()
        }else {
            mMsgBadge.hide()
        }
    }
}